import { getAllStepsOfSuiteInPlan } from './getData'
import { notify } from './notify'
import { NotifyType } from '../common/enums'
import { store } from '../stores/home'
import { genProgressSetActiveSuiteStepList } from '../actions/home'

const cache = new Map()

export const doGetAllStepsOfSuiteInPlan = ({ planId, suiteId }, opt) => {
  const key = `${planId}_${suiteId}`
  const getStepsPromise = cache.has(key) ? Promise.resolve(cache.get(key)) : getAllStepsOfSuiteInPlan({ planId, suiteId }, opt)

  getStepsPromise
    .then(steps => {
      store.dispatch(genProgressSetActiveSuiteStepList(steps))
      cache.set(key, steps)
    })
    .catch(err => notify(NotifyType.error, err))
}